package com.project.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.project.entity.Asset;
import com.project.entity.AssetStatus;
import com.project.entity.AssetType;
import com.project.service.AssetService;
import com.project.service.AssetStatusService;
import com.project.service.AssetTypeService;
import com.project.util.CustomBean;
import com.project.util.EchartsBeans;
import com.project.util.R;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 首页信息控制器
 *
 * @author Leo高洋
 * @create 2023-02-21 1:59
 */
@RestController
@RequestMapping("/home")
public class HomeController {
    @Autowired
    private AssetService assetService;

    @Autowired
    private AssetTypeService assetTypeService;

    @Autowired
    private AssetStatusService assetStatusService;

    private final static Logger logger = LoggerFactory.getLogger(HomeController.class);

    /**
     * 获取首页四卡片信息
     *
     * @return
     */
    @GetMapping("/info")
    public R info() {
        Map<String, Object> map = new HashMap<>();
        Map<String, Object> map2 = new HashMap<>();
        map2.put("sts", 2);
        long assetsTotal = assetService.getTotal(map);
        long assetsFixTotal = assetService.getTotal(map2);
        Integer count = assetTypeService.getCount();
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("assetsTotal", assetsTotal);
        resultMap.put("assetsFixTotal", assetsFixTotal);
        resultMap.put("typeTotal", count);
        return R.ok(resultMap);
    }

    /**
     * 宁夏档案馆固定资产设备类别图
     *
     * @return
     */
    @GetMapping("/typeCount")
    public R typeCount() {
        logger.info("宁夏档案馆固定资产设备类别饼图");
        List<Asset> assets = assetService.typeCount();
        for (Asset asset : assets) {
            AssetType assetType = assetTypeService.findById(String.valueOf(asset.getTypeId()));
            asset.setName(assetType.getName());
        }
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("assetType", assets);
        return R.ok(resultMap);
    }

    /**
     * 宁夏档案馆固定资产设备基本图
     *
     * @return
     */
    @GetMapping("/stsCount")
    public R stsCount() {
        logger.info("宁夏档案馆固定资产设备基本饼图");
        List<Asset> assets = assetService.stsCount();
        for (Asset asset : assets) {
            AssetStatus assetStatus = assetStatusService.findById(String.valueOf(asset.getSts()));
            asset.setStsName(assetStatus.getName());
        }
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("assetStatus", assets);
        return R.ok(resultMap);
    }

    /**
     * 宁夏档案馆固定资产设备基本图
     *
     * @return
     */
    @GetMapping("/assetAddStack")
    public R assetAddStack() {
        logger.info("宁夏档案馆固定资产月增加堆叠折线图");
        List<EchartsBeans> resultList = assetService.findDate();
        for (EchartsBeans eb : resultList) {
            List<CustomBean> list = assetService.selectComplex(eb.getData());
            eb.setDataList(list);
        }
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("resultList", resultList);
        return R.ok(resultMap);
    }
}
